﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace CookEdit
{
    public partial class AddAndModify : Form
    {
        public PlanUnit planUnit = new PlanUnit();

        public AddAndModify()
        {
            InitializeComponent();
            dateTimePicker1.MinDate = DateTime.Now;
            dateTimePicker1.CustomFormat = "yyyy-MM-dd";
            dateTimePicker1.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
            dateTimePicker2.CustomFormat = "HH:mm";
            dateTimePicker2.Format = System.Windows.Forms.DateTimePickerFormat.Custom; 
            dateTimePicker2.ShowUpDown = true;
        }


        public DialogResult ShowFor()
        {
            AddMode = true;
            this.Text = "Add";
            return this.ShowDialog();
        }

        public DialogResult ShowFor(string time,string dir)
        {
            AddMode = false;
            this.Text = "Modify";
            dateTimePicker1.Value = DateTime.ParseExact(time, "yyyyMMddHHmm", System.Globalization.CultureInfo.CurrentCulture);
            dateTimePicker2.Value = DateTime.ParseExact(time, "yyyyMMddHHmm", System.Globalization.CultureInfo.CurrentCulture);
            textBox1.Text = dir;
            return this.ShowDialog();
        }

        public bool AddMode
        {
            get { return button2.Visible; }
            set
            {
                button2.Visible = value;
                button4.Visible = !value;
                this.AcceptButton = value ? button2 : button4;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog dialog = new OpenFileDialog())
			{
				dialog.Filter = "RCP Files (*.rcp)|*.rcp|All Files (*.*)|*.*";
				dialog.FilterIndex = 0;
				if (DialogResult.OK == dialog.ShowDialog())
				{
					textBox1.Text =  dialog.FileName;					
				}
            }
        }

        private void update()
        {
            planUnit.datetime = dateTimePicker1.Value.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo);
            planUnit.datetime += dateTimePicker2.Value.ToString("HHmm", DateTimeFormatInfo.InvariantInfo);
            planUnit.directory = textBox1.Text;           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            update();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            update();
        }
    }
}
